package com.dtagri.fjwlw.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.MenuAdapter;
import com.dtagri.fjwlw.base.BaseFragment;
import com.dtagri.fjwlw.bean.TjMenu;
import com.dtagri.fjwlw.ui.LoginActivity;
import com.dtagri.fjwlw.util.TToast;
import com.dtagri.fjwlw.widget.CommonDialog;
import com.dtagri.fjwlw.widget.DialogHelper;

/**
 * 主页面
 * 
 * @author wxjn
 * 修改人:CL.
 * 最近修改日期:2015-12-07
 */
public class MainFragment extends BaseFragment {

	private ListView listView;
	private MenuAdapter menuAdapter;
	private List<TjMenu> tjMenus;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View rootView = inflater.inflate(R.layout.layout_main_menu, container,
				false);
		initView(rootView);
		return rootView;

	}

	@Override
	public void initView(View view) {
		super.initView(view);
		listView = (ListView) view.findViewById(R.id.listview2);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (!AppContext.isLogin()) {
					showLoginDialog();
				} else {
					TjMenu tjMenu = tjMenus.get(arg2);
					ZMenuEnum pageByTitle = ZMenuEnum.getPageByTitle(tjMenu.getUrl());
					Intent intent = new Intent(getActivity(), pageByTitle
							.getClz());
					startActivity(intent);
				}
			}
		});
		tjMenus = AppContext.user.getTjMenus();
		if (tjMenus == null || tjMenus.size() == 0) {// 未登录，菜单为空
			tjMenus = getVirtualMenu();
		}
		menuAdapter = new MenuAdapter(getActivity(), tjMenus);
		listView.setAdapter(menuAdapter);
	}

	/**
	 * （用户未登录时）虚构菜单
	 * 
	 * @return
	 */
	private List<TjMenu> getVirtualMenu() {
		ArrayList<TjMenu> arrayList = new ArrayList<TjMenu>();
		arrayList.add(new TjMenu("monitoringalarm", "监测报警", "展示当前报警节点及报警数据"));
		arrayList.add(new TjMenu("realtimedata", "实时数据", "实时显示监测节点的采集数据"));
		arrayList.add(new TjMenu("devicecontrol", "设备控制", "实时控制监测节点的外接设备"));
		arrayList.add(new TjMenu("historicaldata", "历史数据", "以图表形式展示历史数据情况"));
		arrayList.add(new TjMenu("indexcontrast", "指标对比", "以曲线形式展示各监测点指标"));
		arrayList.add(new TjMenu("videocenter", "视频中心", "监测节点视频展示及云台控制"));
		return arrayList;
	}

	private void showLoginDialog() {
		final CommonDialog dlg = DialogHelper
				.getPinterestDialogCancelable(getActivity());
		dlg.setTitle("温馨提示");
		dlg.setMessage("需要登录才能使用，是否现在登录?");
		dlg.setNegativeButton("取消", null);
		dlg.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				Intent intent = new Intent(getActivity(), LoginActivity.class);
				startActivity(intent);
				dlg.dismiss();
			}
		});
		dlg.show();
	}
}